//
//  SlideViewController.swift
//  SlideMenu
//
//  Created by admin on 14/9/28.
//  Copyright (c) 2014年 sinosun. All rights reserved.
//

import UIKit

@objc protocol SlideViewDelegate{
    func sideMenuDidSelectItemAtIndex(index:Int)
    optional func sideMenuWillOpen()
    optional func sideMenuWillClose()
}






class SlideViewController: UIViewController, MenuTableViewControllerDelegate{
    
    var leftViewController : LeftViewController?
    var rootViewController : UIViewController!
    
    
    var controllers : [UIViewController]!
    
    var data : [String]!
    
    var delegate : SlideViewDelegate?
    
    var offset_width : CGFloat? = 180
    
    var nav: UINavigationController!
    var is_open_left: Bool = false
    
    
    
    required init(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override init() {
        super.init()
    }
    
    override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: NSBundle?) {
        super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
    }
    
    
    init(controllers rocs: [UIViewController], data title_data: [String]){
        super.init()

        self.leftViewController =  LeftViewController()
        self.data = title_data
        self.leftViewController?.data = title_data
        
        
        self.rootViewController = rocs[0]
        self.controllers = rocs
        
        self.setViews(controllers: controllers, leftviewcontroller: leftViewController, index: 0)
    }
    
    
    
    func setViews(controllers rocs: [UIViewController], leftviewcontroller lc: LeftViewController?, index i : Int){
        self.rootViewController = rocs[i]
        
        nav = UINavigationController(rootViewController: rootViewController)
        rootViewController.navigationController?.navigationBar.barStyle = .Default
        nav.title = self.data[i]
        
        if (leftViewController != nil) {
            leftViewController?.view.frame = CGRectMake(0, 0, self.offset_width!, SCREEN_HEIFHT)
            leftViewController?.view.backgroundColor = UIColor.darkGrayColor()
            
            self.view.addSubview(leftViewController!.view)
            
            //加载图片的方式需要修改一下
            rootViewController.navigationItem.leftBarButtonItem = UIBarButtonItem(image: UIImage(named: "1411987380_editor_list_view_hambuger_menu_-20"), style: UIBarButtonItemStyle.Bordered, target: self, action: "leftShow:")
            leftViewController?.delegate = self
        }
    
        self.view.addSubview(nav.view)
    }



    
    
    
    func leftShow(sender : AnyObject){
        var w : CGFloat = is_open_left  ? 0 : self.offset_width!
        UIView.beginAnimations("leftshow", context: nil)
        nav.view.frame = CGRectMake(w, 0, SCREEN_WIDTH, SCREEN_HEIFHT)
        UIView.setAnimationDuration(1.0)
        UIView.commitAnimations()
        is_open_left = !is_open_left
    }
    
    

    override func viewDidLoad() {
        super.viewDidLoad()


    }
    
    


    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    
    // MARK: - SlideViewDelegate
    func menuControllerDidSelectRow(indexPath:NSIndexPath){
        //self.delegate?.sideMenuDidSelectItemAtIndex(indexPath.row)
        self.leftShow(NSNull())
        rootViewController.view.removeFromSuperview()
        self.setViews(controllers: controllers, leftviewcontroller: leftViewController, index: indexPath.row)
    }
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepareForSegue(segue: UIStoryboardSegue!, sender: AnyObject!) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
